﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PhiStore.Security
{
    internal partial class CredentialOverviewUI : Form
    {
        private bool updating = false; //updating UI - don't run eventhandlers

        public CredentialOverviewUI()
        {
            InitializeComponent();
            CredentialList.ColumnCount = 2;

            CredentialList.Columns[0].Name = "Username";
            CredentialList.Columns[1].Name = "Server Credentials";

            foreach (DataGridViewColumn col in CredentialList.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            VerifyUI();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddCredentials_Click(object sender, EventArgs e)
        {
            CredentialsUI credUI = new CredentialsUI();
            if (credUI.ShowDialog() == DialogResult.OK)
            {
                CredentialManager.Instance.AddCredentials(credUI.Credentials);
                VerifyUI();
            }
        }

        private void ChangeCredentials_Click(object sender, EventArgs e)
        {
            if (CredentialList.SelectedCells.Count > 0)
            {
                PhiCredentials cred = list[CredentialList.SelectedCells[0].RowIndex];

                CredentialsUI credUI = new CredentialsUI(cred);
                if (credUI.ShowDialog() == DialogResult.OK)
                {
                    CredentialManager.Instance.ChangeCredentials(cred, credUI.Credentials);
                    VerifyUI();
                }
            }
        }
        private void CredentialList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            if (CredentialList.HitTest(e.X, e.Y).RowIndex >= 0 
                && ChangeCredentials.Enabled)
            {
                ChangeCredentials_Click(sender, e);
            }
        }

        private void RemoveCredentials_Click(object sender, EventArgs e)
        {
            if(CredentialList.SelectedCells.Count > 0){
                PhiCredentials cred = list[CredentialList.SelectedCells[0].RowIndex];

                CredentialManager.Instance.RemoveCredentials(cred);
                VerifyUI();
            }
        }

        private void AnonymousClients_CheckedChanged(object sender, EventArgs e)
        {
            if (updating) return;

            if (AnonymousClients.Checked && !AnonymousServers.Checked)
            {
                CredentialManager.Instance.RemoveCredentials(new ServerCredentialContainer());
                CredentialManager.Instance.AddCredentials(new ClientCredentials());
            }
            else
            {
                CredentialManager.Instance.RemoveCredentials(new ClientCredentials());
            }
            VerifyUI();
        }

        private void AnonymousServers_CheckedChanged(object sender, EventArgs e)
        {
            if (updating) return;

            if (AnonymousServers.Checked)
            {
                CredentialManager.Instance.RemoveCredentials(new ClientCredentials());
                CredentialManager.Instance.AddCredentials(new ServerCredentialContainer());
            }
            else
            {
                CredentialManager.Instance.RemoveCredentials(new ServerCredentialContainer());
            }
            VerifyUI();
        }

        List<PhiCredentials> list = new List<PhiCredentials>();
        private void VerifyUI()
        {
            updating = true;

            list = new List<PhiCredentials>(CredentialManager.Instance.GetAllCredentials());
            CredentialList.Rows.Clear();

            foreach (PhiCredentials cred in list)
            {
                string[] row = new string[] { cred.Username.ToString(), cred.IsServerCredential.ToString() };
                if (row[0] == "") row[0] = "Anonymous";

                CredentialList.Rows.Add(row);
            }

            if (CredentialManager.Instance.VerifyCredentials(new ServerCredentialContainer()))
            { //anonymous servers
                AnonymousClients.Checked = true;
                AnonymousClients.Enabled = false;
                AnonymousServers.Checked = true;

            }
            else
            {
                if (CredentialManager.Instance.VerifyCredentials(new ClientCredentials()))
                { // anonymous clients
                    AnonymousClients.Checked = true;
                    AnonymousClients.Enabled = true;
                    AnonymousServers.Checked = false;
                }
                else
                {
                    AnonymousClients.Checked = false;
                    AnonymousClients.Enabled = true;
                    AnonymousServers.Checked = false;
                }

            }

            updating = false;
        }
    }
}
